package com.bupt.liutong.front.shop.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bupt.liutong.admin.core.base.BaseAction;
import com.bupt.liutong.admin.core.bo.UserBo;
import com.bupt.liutong.admin.core.form.UserForm;
import com.bupt.liutong.admin.shop.bo.CourseBo;
import com.bupt.liutong.admin.shop.form.ProdForm;
import com.bupt.liutong.admin.shop.form.ProdPhotoForm;
import com.bupt.liutong.util.BeanUtils;
import com.bupt.liutong.util.StringUtils;

public class CourseAction extends BaseAction {
	
	private CourseBo courseBo;
	private UserBo userBo;
	
	public ActionForward index(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("shop_course_intro", courseBo.getIntro());
		return mapping.findForward("index");
	}

	public ActionForward intro(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("shop_course_intro", courseBo.getIntro());
		return mapping.findForward("intro");
	}
	
	public ActionForward list(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		prodForm.setPageSize(12);
		prodForm.setEnabled(1);
		List<ProdForm> prodFormList = courseBo.getCoursesByTypeIdByPage(prodForm);
		/*
		// des
		if (prodFormList != null && prodFormList.size() == 1) {
			BeanUtils.copyProperties(prodForm, prodFormList.get(0));
			List<ProdPhotoForm> photoFormList = courseBo.getCoursePhotosById(prodForm);
			if (photoFormList == null || photoFormList.size() == 0)
				return mapping.findForward("desNoAlbum");
			else {
				request.setAttribute("photoFormList", photoFormList);
				return mapping.findForward("desAlbum");
			}
		}
		// list
		else {
		*/
		request.setAttribute("prodFormList", prodFormList);
		request.setAttribute("listTitle", courseBo.getTypeNameByTypeId(prodForm));
		putPageInfo(request, prodForm);
		return mapping.findForward("list");
	}
	
	public ActionForward hotList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		prodForm.setPageSize(12);
		prodForm.setEnabled(1);
		request.setAttribute("prodFormList", courseBo.getHotCoursesByPage(prodForm));
		request.setAttribute("listTitle", "最新课程");
		putPageInfo(request, prodForm);
		//request.setAttribute("anotherPage", "<a href='../course.do?method=allList&searchFlag=1' style='color:white;text-decoration:underline;'>查看所有课程</a>");
		return mapping.findForward("hotList");
	}
	
	public ActionForward allList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		prodForm.setPageSize(12);
		prodForm.setEnabled(1);
		request.setAttribute("prodFormList", courseBo.getCoursesByPage(prodForm));
		request.setAttribute("listTitle", "所有课程");
		request.setAttribute("anotherPage", "<a href='../course.do?method=hotList&searchFlag=1' style='color:white;text-decoration:underline;'>查看最新课程</a>");
		putPageInfo(request, prodForm);
		return mapping.findForward("allList");
	}
	
	public ActionForward profCourses(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		prodForm.setPageSize(12);
		prodForm.setEnabled(1);
		request.setAttribute("prodFormList", courseBo.getProfCoursesByPage(prodForm));
		request.setAttribute("listTitle", "专业课程");
		request.setAttribute("anotherPage", "<a href='../course.do?method=hotList&searchFlag=1' style='color:white;text-decoration:underline;'>查看最新课程</a>");
		putPageInfo(request, prodForm);
		return mapping.findForward("allList");
	}
	
	public ActionForward unProfCourses(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		prodForm.setPageSize(12);
		prodForm.setEnabled(1);
		request.setAttribute("prodFormList", courseBo.getUnProfCoursesByPage(prodForm));
		request.setAttribute("listTitle", "休闲课程");
		request.setAttribute("anotherPage", "<a href='../course.do?method=hotList&searchFlag=1' style='color:white;text-decoration:underline;'>查看最新课程</a>");
		putPageInfo(request, prodForm);
		return mapping.findForward("allList");
	}
	
	public ActionForward des(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		BeanUtils.copyProperties(prodForm, courseBo.getCourseById(prodForm));
		List<ProdPhotoForm> photoFormList = courseBo.getCoursePhotosById(prodForm);
		request.setAttribute("attrFormList", courseBo.getAttrs());
		request.setAttribute("attrValMap", courseBo.getAttrVals(prodForm));
		if (photoFormList == null || photoFormList.size() == 0)
			return mapping.findForward("desNoAlbum");
		else {
			request.setAttribute("photoFormList", photoFormList);
			return mapping.findForward("desAlbum");
		}
	}
	

	public ActionForward signup(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		UserForm sessionForm = (UserForm) request.getSession().getAttribute("userForm");
		if (sessionForm == null
				|| StringUtils.isEmpty(sessionForm.getUserName())) {
			ActionForward forward = mapping.findForward("login");
			String path = forward.getPath()+"?redirectUrl=/front/shop/course.do?method=signup%26prodId=" + request.getParameter("prodId");
			return new ActionForward(forward.getName(), path, forward.getRedirect(), forward.getContextRelative());
		} else {
			request.setAttribute("prodId", request.getParameter("prodId"));
			return mapping.findForward("signup");
		}
	}
	
	/**
	 * 课程报名
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward signupDo(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		UserForm userForm = new UserForm();
		UserForm sessionForm = (UserForm) request.getSession().getAttribute("userForm");
		userForm.setUserName(sessionForm.getUserName());
		userForm.setRealName(request.getParameter("realName"));
		userForm.setIdCard(request.getParameter("idCard"));
		userForm.setMobilePhone(request.getParameter("mobilePhone"));
		userBo.updateUser(userForm);
		String path = mapping.findForward("insertProdCart").getPath() + "&prodId="
				+ request.getParameter("prodId") + "&prodNum=1";
		return new ActionForward(path, true);
	}

	public void setCourseBo(CourseBo courseBo) {
		this.courseBo = courseBo;
	}

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}
}
